package com.dataaccess.dataaccess.web.web;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.util.HexUtil;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.utils.appkit.FirefightingiotKit;
import com.dataaccess.dataaccess.config.rocketmq.UserProducer;
import com.dataaccess.dataaccess.model.AlarmAllDeviceDataChanged;
import com.dataaccess.dataaccess.support.annotaion.Action;
import com.dataaccess.dataaccess.support.annotaion.At;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.support.utils.MyLogger;
import com.dataaccess.dataaccess.web.mapper.AlarmAllDeviceDataChangedMapper;
import com.dataaccess.dataaccess.web.service.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @Author: YangYu
 * @ClassName: DataController
 * @Date: Created in 11:13 2023/7/7
 * @Description: tcp设备数据接受
 */

@Action
@RequestMapping("device")
public class DataController {
    @Resource
    public DeviceAboatFirewaterService deviceAboatFirewaterService;
    @Resource
    private AlarmAllDeviceDataChangedMapper alarmAllDeviceDataChangedMapper;
    @Resource
    private AutoNetworkAlarmDeviceDataChangedService autoNetworkAlarmDeviceDataChangedService;
    @Resource
    private DataService dataService;
    @Resource
    private DeviceAboatElectricService deviceAboatElectricService;
    @Resource
    private DeviceAboatFireExtinguisherService deviceAboatFireExtinguisherService;
    @Resource
    private DeviceAboatGasService deviceAboatGasService;
    @Resource
    public MyLogger logger;
    @Resource
    private UserProducer userProducer;

    /**
     * @Author YangYu
     * @Description 数据接受
     * @Date Created in 11:15 2023/7/7
     * @Param [jsonParam]
     */

    @At(value = "dataInfo", method = RequestMethod.POST)
    public RespEntity getInfo(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        String data = HexUtil.encodeHexStr(Base64Decoder.decode(jsonParam.getStr("code")));
        //String data =jsonParam.getStr("code");
        jsonParam.put("code", data);
        //报警数据
        AlarmAllDeviceDataChanged alarmAllDeviceDataChanged = new AlarmAllDeviceDataChanged();
        alarmAllDeviceDataChanged.setId(UUID.randomUUID().toString());
        alarmAllDeviceDataChanged.setJsondata(jsonParam.toString());
        alarmAllDeviceDataChanged.setType("dataInfo");
        alarmAllDeviceDataChangedMapper.insert(alarmAllDeviceDataChanged);
        //水柱箱体
        if (data.contains("404055aa")) {
            if (data.length() == 80) {
                deviceAboatFirewaterService.waterColumnBox(jsonParam);
            }
            //互联通电设备630
        } else if (data.length() == 286) {
            deviceAboatElectricService.hltElectric630(jsonParam);
            //互联通电设备640
        } else if (data.length() == 228) {
            deviceAboatElectricService.hltElectric640(jsonParam);
        } else if (data.substring(0, 4).equals("6060")) {
            deviceAboatFireExtinguisherService.gasFireExtinguishingData(jsonParam);
        } else if (data.substring(0, 4).equals("7070")) {
            deviceAboatGasService.jdy_gas(jsonParam);
        } else if (data.substring(0, 4).equals("7f7f")) {
            deviceAboatFirewaterService.zrMeter(jsonParam);
        }
        //诸城水表平台用
        //dataService.water_meter(jsonParam);
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 风压监测系统（京电云）
     * @Date Created in 17:10 2024/1/15
     * @Param [jsonParam]
     */

    @At(value = "windPressure", method = RequestMethod.POST)
    public RespEntity wirelessInstrument(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        logger.info("windPressure"+"------"+jsonParam);
        jsonParam.put("deviceFlag","windPressure");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            return dataService.windPressure(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }
}
